package com.fh.controller.system.weixin.wxmenu;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Properties;
import net.sf.json.JSONObject;
import com.fh.controller.system.weixin.wxutil.MenuUtil;
import com.fh.controller.system.weixin.wxutil.WeixinUtil;


/**
 * 菜单管理
 *
 * @author why
 *
 */
public class MenuManager {

    /**
     * 定义菜单结构
     *
     * @param
     */
    private static Menu getMenu() {
        ClickButton btn1s1 = new ClickButton();
        btn1s1.setName("扫码带提示");
        btn1s1.setKey("rselfmenu_0_0");
        btn1s1.setType("scancode_waitmsg");

        String redirect_url = "http://xiangyunfei.tunnel.mobi/FISHING/weixin/get_code";

        // String url1 =
        // "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx7afbbd5432be9c7d&redirect_uri="+URLEncoder.encode(redirect_url)+"&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";

        // String url2 =
        // "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx7afbbd5432be9c7d&redirect_uri="+URLEncoder.encode(redirect_url)+"&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
        String url1 = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
                + WeixinUtil.appId
                + "&redirect_uri="
                + URLEncoder.encode(redirect_url)
                + "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";

        // 获取openid及用户信息
        String url2 = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
                + WeixinUtil.appId
                + "&redirect_uri="
                + URLEncoder.encode(redirect_url)
                + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
        String url3 = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
                + WeixinUtil.appId
                + "&redirect_uri="
                + URLEncoder.encode(redirect_url)
                + "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";

        ViewButton btn1s2 = new ViewButton();
        btn1s2.setName("获取openId000");
        btn1s2.setType("view");
        // btn1s2.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx2a31085e3aab06ce&redirect_uri=http://b51f09bb.ngrok.io/WeiXinInterface/weixin/get_code&response_type=code&scope=snsapi_base&state=123#wechat_redirect");
        btn1s2.setUrl(url1);

        ViewButton btn1s3 = new ViewButton();
        btn1s3.setName("获取openId3");
        btn1s3.setType("view");
        // btn1s3.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx2a31085e3aab06ce&redirect_uri=http://b51f09bb.ngrok.io/WeiXinInterface/weixin/get_code&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect");
        btn1s3.setUrl(url3);

        ComplexButton btn1s = new ComplexButton();
        btn1s.setName("扫码");
        btn1s.setSub_button(new Button[] { btn1s1, btn1s2, btn1s3 });

        ClickButton btn2s1 = new ClickButton();
        btn2s1.setName("系统拍照发图");
        btn2s1.setKey("rselfmenu_1_0");
        btn2s1.setType("pic_sysphoto");

        ClickButton btn2s2 = new ClickButton();
        btn2s2.setName("拍照或者相册发图");
        btn2s2.setKey("rselfmenu_1_1");
        btn2s2.setType("pic_photo_or_album");

        ClickButton btn2s3 = new ClickButton();
        btn2s3.setName("微信相册发图");
        btn2s3.setKey("rselfmenu_1_2");
        btn2s3.setType("pic_weixin");

        ViewButton btn2s4 = new ViewButton();
        btn2s4.setName("获取openId2");
        btn2s4.setType("view");
        btn2s4.setUrl("http://rzht888.tunnel.mobi/fishing/weixin/scan");

        ComplexButton btn2s = new ComplexButton();
        btn2s.setName("发图");
        btn2s.setSub_button(new Button[] { btn2s1, btn2s2, btn2s3, btn2s4 });

        ClickButton btn3s = new ClickButton();
        btn3s.setName("发送位置");
        btn3s.setKey("location_select");
        btn3s.setType("location_select");

        // ComplexButton btn3s = new ComplexButton();
        // btn3s.setName("位置");
        // btn3s.setSub_button(new Button[] {btn3s1});

        Menu menu = new Menu();
        menu.setButton(new Button[] { btn1s, btn2s, btn3s });
        // menu.setButton(new Button[]{btn1s});
        String jsonMenu = JSONObject.fromObject(menu).toString();
        System.out.println("创建的菜单\n\n\n" + jsonMenu);
        return menu;
    }

    private static Menu getFishingMenu() {
        // 域名
        String URL = "";
        MenuManager loadProp = new MenuManager();
        InputStream in = loadProp.getClass().getResourceAsStream(
                "/words.properties");
        Properties prop = new Properties();
        try {
            prop.load(in);
            URL = prop.getProperty("wd_URL");
            System.out.println("URL" + URL);
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            prop.clear();
        }
        String redirect_url = "http://gxz.tunnel.mobi/FISHING/userInfo/sponsorPK";
        // 获取openid
        String url1 = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
                + WeixinUtil.appId
                + "&redirect_uri="
                + URLEncoder.encode(redirect_url)
                + "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
        // 获取openid及用户信息
        String url2 = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
                + WeixinUtil.appId
                + "&redirect_uri="
                + URLEncoder.encode(redirect_url)
                + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect";

        // 面对面PK
        String urlPk = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
                + WeixinUtil.appId
                + "&redirect_uri="
                + URLEncoder.encode(URL + "userInfo/sponsorPK")
                + "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
        // 扫描PK
        String urlScanPK = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
                + WeixinUtil.appId
                + "&redirect_uri="
                + URLEncoder.encode(URL + "userInfo/scan")
                + "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
        // 上报PK结果
        String urlUploadPK = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
                + WeixinUtil.appId
                + "&redirect_uri="
                + URLEncoder.encode(URL + "userInfo/reportPKList")
                + "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
        // 找人PK
        String urlFindPk = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
                + WeixinUtil.appId
                + "&redirect_uri="
                + URLEncoder.encode(URL + "pk/toPkSearch")
                + "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
        // 预约PK
        String urlOrderPk = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
                + WeixinUtil.appId
                + "&redirect_uri="
                + URLEncoder.encode(URL + "pk/toReservation")
                + "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";

        // 积分兑换
        String urlScoreChange = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
                + WeixinUtil.appId
                + "&redirect_uri="
                + URLEncoder.encode(URL + "fishingexchangegoods/goods_list")
                + "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
        // 活动介绍
        String urlActivity = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
                + WeixinUtil.appId
                + "&redirect_uri="
                + URLEncoder.encode(URL + "findUser/fish_acticle")
                + "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";

        // 积分卡==我的个人中心
        // String
        // urlCard="https://open.weixin.qq.com/connect/oauth2/authorize?appid="+WeixinUtil.appId+"&redirect_uri="+URLEncoder.encode(redirect_url)+"&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
        // 擂台加油站
        String urlFinshStation = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
                + WeixinUtil.appId
                + "&redirect_uri="
                + URLEncoder.encode(URL + "gasStation/info")
                + "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
        // 积分卡==我的个人中心
        String urlPersonalInfo = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
                + WeixinUtil.appId
                + "&redirect_uri="
                + URLEncoder.encode(URL + "personal/toInfo")
                + "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
        // 签到
        String urlSign = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
                + WeixinUtil.appId
                + "&redirect_uri="
                + URLEncoder.encode(URL + "personal/toSign")
                + "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
        // ================================PK专区菜单========================start
        // 面对面PK菜单定义
        ViewButton btn1s1 = new ViewButton();
        btn1s1.setName("面对面PK");
        btn1s1.setType("view");
        btn1s1.setUrl(urlPk);
        // 扫描接受PK
        ViewButton btn1s2 = new ViewButton();
        btn1s2.setName("扫描接受pk");
        btn1s2.setType("view");
        btn1s2.setUrl(urlScanPK);
        // 上报PK结果
        ViewButton btn1s3 = new ViewButton();
        btn1s3.setName("上报PK结果");
        btn1s3.setType("view");
        btn1s3.setUrl(urlUploadPK);
        // 找人PK
        ViewButton btn1s4 = new ViewButton();
        btn1s4.setName("找人PK");
        btn1s4.setType("view");
        btn1s4.setUrl(urlFindPk);
        // 预约PK

        ViewButton btn1s5 = new ViewButton();
        btn1s5.setName("预约PK");
        btn1s5.setType("view");
        btn1s5.setUrl(urlOrderPk);
        // PK专区菜单组
        ComplexButton btn1s = new ComplexButton();
        btn1s.setName("PK专区");
        btn1s.setSub_button(new Button[] { btn1s1, btn1s2, btn1s3, btn1s4,
                btn1s5 });
        // ================================PK专区菜单========================end

        // ====================积分兑换===============================start
        ViewButton btn2s1 = new ViewButton();
        btn2s1.setName("积分兑换");
        btn2s1.setType("view");
        btn2s1.setUrl(urlScoreChange);

        ViewButton btn2s2 = new ViewButton();
        btn2s2.setName("活动介绍");
        btn2s2.setType("view");
        btn2s2.setUrl(urlActivity);

		/*
		 * ViewButton btn2s3 = new ViewButton(); btn2s3.setName("scan");
		 * btn2s3.setType("view");
		 * btn2s3.setUrl("http://jianlk.tunnel.mobi/DY/weixin/scan");
		 */
        // PK专区菜单组
        ComplexButton btn2s = new ComplexButton();
        btn2s.setName("积分兑换");
        btn2s.setSub_button(new Button[] { btn2s1, btn2s2 });
        // ====================积分兑换================================end

        // ====================个人中心================================start
        ViewButton btn3s1 = new ViewButton();
        btn3s1.setName("积分卡");
        btn3s1.setType("view");
        btn3s1.setUrl(urlPersonalInfo);

        ViewButton btn3s2 = new ViewButton();
        btn3s2.setName("擂台加油站");
        btn3s2.setType("view");
        btn3s2.setUrl(urlFinshStation);

		/*
		 * ViewButton btn3s3 = new ViewButton(); btn3s3.setName("个人中心");
		 * btn3s3.setType("view"); btn3s3.setUrl(urlPersonalInfo);
		 */

        ViewButton btn3s3 = new ViewButton();
        btn3s3.setName("签到");
        btn3s3.setType("view");
        btn3s3.setUrl(urlSign);
        // PK专区菜单组
        ComplexButton btn3s = new ComplexButton();
        btn3s.setName("我的");
        btn3s.setSub_button(new Button[] { btn3s1, btn3s2, btn3s3 });
        // ====================个人中心================================end
        Menu menu = new Menu();
        menu.setButton(new Button[] { btn1s, btn2s, btn3s });
        String jsonMenu = JSONObject.fromObject(menu).toString();
        System.out.println("创建的菜单\n\n\n" + jsonMenu);
        return menu;
    }

    public static void main(String[] args) {
        String appId = WeixinUtil.getAppId();
        String appSecret = WeixinUtil.getAppsecret();

        // 生成token
        String token = WeixinUtil.getAccessToken(appId, appSecret).getToken();
        // String token =
        // "v5va5uuWUmOC4MLz2UseNXgT37L-ZmSrURqeAhfNs-AkoVN4YNMj9xKa2S1ZzVu7UEBbrXckeHtvXSESSlDhXJvqDWlOhz4izR4DMTfXdGM";
        if (null != token) {
            // 创建菜单
            boolean result = MenuUtil.createMenu(getFishingMenu(), token);
            if (result) {
                System.out.println("菜单创建成功");
            } else {
                System.out.println("菜单那创建失败");
            }
        }

        // http%3A%2F%2F027e5124.ngrok.io%2FFISHING%2Fos.do
        // http%3A%2F%2F027e5124.ngrok.io%2FFISHING%2Fos.do
        // String url = "http://027e5124.ngrok.io/FISHING/os.do";
        // try {
        // System.out.println(URLEncoder.encode(url, "UTF-8"));
        // } catch (Exception e) {
        // e.printStackTrace();
        // }
    }

}
